﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Web.Routing;
using System.Configuration;
using Spring.Context.Support;
using Spring.Web.Mvc;
using EIIS.Infrastructure.Model.System;
using EIIS.Infrastructure.Service.Implement;

namespace EIIS.Presentation.Controllers
{
    public class Global : SpringMvcApplication
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger("Logger");

        protected override void Application_Start(object sender, EventArgs e)
        {
            //初始化log4net
            Uri uri = new Uri(this.Server.MapPath("~/Config/log4net.xml"));
            log4net.Config.XmlConfigurator.Configure(uri);            

            base.Application_Start(sender, e);            
        }

        

        protected override void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.ico/{*pathInfo}");

            //routes.MapRoute(
            //    "DicCategory",                                         // 路由名称
            //    "DicCategory/{action}/{id}",                        // 带有参数的 URL
            //    new { controller = "DicCategory", action = "Index", id = UrlParameter.Optional }   // 参数默认值
            //);

            //routes.MapRoute(
            //    "Dictionary",                                         // 路由名称
            //    "Dictionary/{action}/{id}",                        // 带有参数的 URL
            //    new { controller = "Dictionary", action = "Index", id = UrlParameter.Optional }   // 参数默认值
            //);

            //routes.MapRoute(
            //    "Package",                                         // 路由名称
            //    "Package/{action}/{id}",                        // 带有参数的 URL
            //    new { controller = "Package", action = "Index", id = UrlParameter.Optional }   // 参数默认值
            //);

            //routes.MapRoute(
            //    "ActionPermission",                                         // 路由名称
            //    "ActionPermission/{action}/{id}",                        // 带有参数的 URL
            //    new { controller = "ActionPermission", action = "Index", id = UrlParameter.Optional }   // 参数默认值
            //);

            //routes.MapRoute(
            //    "Menu",                                         // 路由名称
            //    "Menu/{action}/{id}",                        // 带有参数的 URL
            //    new { controller = "Menu", action = "Index", id = UrlParameter.Optional }   // 参数默认值
            //);

            routes.MapRoute(
                "parameter",                                         // 路由名称
                "{controller}/{action}/{id}",                        // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }   // 参数默认值
            );

            routes.MapRoute(
                "default",                                         // 路由名称
                "{controller}/{action}",                        // 带有参数的 URL
                new { controller = "Home", action = "Index" }   // 参数默认值
            );           

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            //if (this.Server.GetLastError() == null)
            //{
            //    return;
            //}

            //Exception ex = this.Server.GetLastError().GetBaseException();
            //StringBuilder sb = new StringBuilder();

            //sb.Append(ex.Message);
            //sb.Append("\r\nSOURCE: " + ex.Source);
            //if (Request.Form != null)
            //{
            //    sb.Append("\r\nFORM: " + this.Request.Form.ToString());
            //}
            //if (Request.QueryString != null)
            //{
            //    sb.Append("\r\nQUERYSTRING: " + this.Request.QueryString.ToString());
            //}

            //sb.Append("\r\n引发当前异常的原因: " + ex.TargetSite);
            //sb.Append("\r\n堆栈跟踪: " + ex.StackTrace);
            //logger.Error(sb.ToString());

            //var key = ConfigurationManager.AppSettings["IsDebug"];
            //bool isDebug;
            //if (!bool.TryParse(key, out isDebug) || !isDebug)
            //{
            //    this.Server.ClearError();
            //}
        }


    }
}
